<template>
  <div class="lexical-analysis">
    <div class="container">
      <div class="analysis-card">
        <div class="card-header">
          <h2>文本分析</h2>
          <div class="language-select">
            <label class="radio-label">
              <input type="radio" v-model="language" value="zh">
              <span class="radio-text">中文</span>
            </label>
            <!-- <label class="radio-label">
              <input type="radio" v-model="language" value="en">
              <span class="radio-text">English</span>
            </label> -->
          </div>
        </div>

        <div class="input-section">
          <textarea 
            v-model="inputText"
            :placeholder="language === 'zh' ? '请输入要分析的文本...' : 'Enter text to analyze...'"
            rows="5"
            class="input-text"
          ></textarea>
          
          <button 
            @click="analyze" 
            :disabled="!inputText"
            class="analyze-btn"
          >
            <span class="btn-text">开始分析</span>
            <span class="btn-icon">→</span>
          </button>
        </div>

        <div v-if="result" class="result-section">
          <div class="result-card tokens">
            <h3>分词结果</h3>
            <div class="token-list">
              <span v-for="(token, index) in result.tokens" 
                    :key="index" 
                    class="token">
                {{ token }}
              </span>
            </div>
          </div>

          <div class="result-card pos-tags">
            <h3>词性标注</h3>
            <div class="pos-tag-list">
              <div v-for="(tag, index) in result.pos_tags" 
                   :key="index" 
                   class="pos-tag">
                <span class="word">{{ tag[0] }}</span>
                <span class="tag">{{ tag[1] }}</span>
              </div>
            </div>
          </div>
        </div>

        <div v-if="error" class="error-message">
          {{ error }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'LexicalAnalysis',
  data() {
    return {
      inputText: '',
      language: 'zh',
      result: null,
      error: null
    }
  },
  methods: {
    async analyze() {
      try {
        this.error = null
        const response = await axios.post('http://localhost:8000/api/analyze/', {
          text: this.inputText,
          language: this.language
        })
        this.result = response.data
      } catch (err) {
        this.error = '分析过程中出现错误：' + (err.response?.data?.error || err.message)
      }
    }
  }
}
</script>

<style scoped>
.lexical-analysis {
  padding-top: 80px;
  min-height: 100vh;
  background-color: var(--bg-color);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.analysis-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
  padding: 2rem;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.card-header h2 {
  margin: 0;
  color: var(--primary-color);
  font-weight: 600;
}

.language-select {
  display: flex;
  gap: 1.5rem;
}

.radio-label {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.radio-text {
  margin-left: 0.5rem;
  font-weight: 500;
}

.input-section {
  margin-bottom: 2rem;
}

.input-text {
  width: 100%;
  padding: 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
  resize: vertical;
  margin-bottom: 1rem;
}

.input-text:focus {
  outline: none;
  border-color: var(--accent-color);
}

.analyze-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 1.5rem;
  background-color: var(--accent-color);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.analyze-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(66, 185, 131, 0.2);
}

.analyze-btn:disabled {
  background-color: #cbd5e0;
  cursor: not-allowed;
}

.btn-icon {
  font-size: 1.2rem;
  transition: transform 0.3s ease;
}

.analyze-btn:hover .btn-icon {
  transform: translateX(4px);
}

.result-section {
  display: grid;
  gap: 2rem;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}

.result-card {
  background: #f8fafc;
  border-radius: 8px;
  padding: 1.5rem;
}

.result-card h3 {
  margin: 0 0 1rem 0;
  color: var(--primary-color);
  font-weight: 600;
}

.token-list, .pos-tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.token {
  background-color: #e2e8f0;
  color: var(--primary-color);
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 0.9rem;
}

.pos-tag {
  background-color: white;
  border: 1px solid #e2e8f0;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.word {
  font-weight: 500;
  color: var(--primary-color);
}

.tag {
  font-size: 0.8rem;
  color: #64748b;
  margin-top: 0.25rem;
}

.error-message {
  margin-top: 1rem;
  padding: 1rem;
  background-color: #fee2e2;
  color: #dc2626;
  border-radius: 8px;
  font-size: 0.9rem;
}

@media (max-width: 768px) {
  .container {
    padding: 1rem;
  }
  
  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
  }
  
  .result-section {
    grid-template-columns: 1fr;
  }
}
</style> 